package cn.youyeku.zsxh.common;

import android.os.Handler;
import android.view.KeyEvent;

import cn.youyeku.zsxh.R;
import cn.youyeku.zsxh.app;
/**
 * Created by wings on 2017/2/20.
 *
 *
 * 减少耦合使用backgroundToast 减少传递的this
 *
 */

public class DoubleClickExitHelper {



    private boolean twicePressed = false;
    private Handler mHandler;

    public DoubleClickExitHelper() {
        mHandler = new Handler();
    }


    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode != KeyEvent.KEYCODE_BACK) {
            return false;
        }
        if (twicePressed) {
            app.appExit();
            return true;
        } else {
            twicePressed = true;
            app.backgroundThreadShortToast(R.string.back_more_exit);
            mHandler.postDelayed(onBackTimeRunnable, 2000);
            return true;
        }
    }
    private Runnable onBackTimeRunnable = new Runnable() {
        @Override
        public void run() {
            twicePressed = false;
        }
    };

}
